export class MathService {

  constructor() { }


  /**
   * 小数点后补零
   * @param value number
   * @param digs number
   * @param byte 0 or any
   */
  padEnd(value: number, digs: number, byte = '0'): string {

    let num: string = String(value);
    const index = num.indexOf('.');

    if (index === -1) {
      num += '.' + Array(digs + 1).join(byte);
    } else {
      const len = digs - this.getDigs(value);
      if (len > 0) {
        num += Array(len + 1).join(byte);
      }
    }

    return num;
  }


  /**
   * 获取小数点后的位数
   * @param value  number
   */
  getDigs(value: number): number {

    const num: string = String(value);
    const index = num.indexOf('.');

    if (index === -1) {
      return 0;
    } else {
      return num.length - 1 - index;
    }
  }

  /**
   * 取值到小数位（四舍五入）
   * @param num number数值
   * @param digs (可选)保留的小数位数
   */
  toFixed(num: number, digs?: number): string {

    if (isNaN(digs) || !isFinite(digs)) {
      return String(num);
    }
    const times: number = Math.pow(10, digs);
    let des: string = String(num * times + 0.5);

    const value = parseInt(des, 10) / times;
    // 补零
    des = this.padEnd(value, digs);

    return des;
  }

  /**
   * Math 加法
   * @param num1 number
   * @param num2 number
   * @param digs (可选)保留的小数位
   */
  add(num1: number, num2: number, digs?: number): string {

    const maxDigs: number = Math.max(this.getDigs(num1), this.getDigs(num2));

    const times: number = Math.pow(10, maxDigs);

    const value: number = (num1 * times + num2 * times) / times;

    return this.toFixed(value, digs);
  }

  /**
   * Math 减法
   * @param num1 number
   * @param num2 number
   * @param digs (可选)保留的小数位
   */
  subtract(num1: number, num2: number, digs?: number): string {

    const maxDigs: number = Math.max(this.getDigs(num1), this.getDigs(num2));

    const times: number = Math.pow(10, maxDigs);

    const value: number = (num1 * times - num2 * times) / times;

    return this.toFixed(value, digs);
  }

  /**
   * Math 乘法
   * @param num1 number
   * @param num2 number
   * @param digs (可选)保留的小数位
   */
  multiply(num1: number, num2: number, digs?: number): string {

    const maxDigs: number = Math.max(this.getDigs(num1), this.getDigs(num2));

    const times: number = Math.pow(10, maxDigs);

    const value: number = (num1 * times * num2 * times) / times;

    return this.toFixed(value, digs);
  }

  /**
   * Math 除法
   * @param num1 number
   * @param num2 number
   * @param digs (可选)保留的小数位
   */
  divide(num1: number, num2: number, digs?: number): string {
    // 分母不能为0
    if (num2 === 0) {
      return 0 + '';
    }

    const maxDigs: number = Math.max(this.getDigs(num1), this.getDigs(num2));

    const times: number = Math.pow(10, maxDigs);

    const value: number = (num1 * times / (num2 * times)) / times;

    return this.toFixed(value, digs);
  }
}